package com.jyy.prefabricated.controller;

import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.ExamPaperGoods;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.service.ExamPaperGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 练习关联项目课程表(ExamPaperGoods)表控制层
 *
 * @author makejava
 * @since 2021-05-28 10:07:03
 */
@Slf4j
@RestController
@RequestMapping("api")
public class ExamPaperGoodsController {
    /**
     * 服务对象
     */
    @Resource
    private ExamPaperGoodsService examPaperGoodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/examPaperGoods")
    public ExamPaperGoods selectOne(Integer id) {
        return this.examPaperGoodsService.queryById(id);
    }

    /**
     * 新增练习关联课程或项目
     *
     * @param examPaperGoods
     * @return
     */
    @PostMapping("/examPaperGoods")
    public AjaxResult insert(@Valid @RequestBody ExamPaperGoods examPaperGoods) {
        ExamPaperGoods service = examPaperGoodsService.insert(examPaperGoods);
        ExamPaperGoods result = examPaperGoodsService.queryById(service.getId());
        return AjaxResult.success(result);
    }

    /**
     * 更新练习关联课程或项目
     *
     * @param examPaperGoods
     * @return
     */
    @PutMapping("/examPaperGoods")
    public AjaxResult update(@Valid @RequestBody ExamPaperGoods examPaperGoods) {

        examPaperGoodsService.update(examPaperGoods);
        ExamPaperGoods result = examPaperGoodsService.queryById(examPaperGoods.getId());
        return AjaxResult.success(result);
    }

    /**
     * 删除练习关联课程或项目
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/examPaperGoods")
    public AjaxResult update(@Valid @RequestBody BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        examPaperGoodsService.batchUpdateDeleted(dto);
        return AjaxResult.success();
    }


    /**
     * 查询练习关联课程或项目详情
     *
     * @param id
     * @return
     */
    @GetMapping("/examPaperGoods/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find course by id:{}", id);
        ExamPaperGoods result = examPaperGoodsService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 查询练习关联课程或项目列表不分页
     *
     * @return
     */
    @GetMapping("/examPaperGoods-list")
    public AjaxResult selectByParams(@Valid ExamPaperGoods examPaperGoods) {

        List<ExamPaperGoods> result = examPaperGoodsService.queryList(examPaperGoods);
        return AjaxResult.success(result);
    }
}
